source('codes/00_import_libraries.R')


#------------------------------------------------------------------------
# Data
#------------------------------------------------------------------------

topic_list = readRDS('input/narrative_dicts_v2.rds')[['base']] %>% names()
topic_list_12 = topic_list %>% tail(-2)

gw = readRDS('input/gw_macro_predictors.RDS')
gw_list = c('DP', 'DY', 'EP', 'DE', 'SVAR', 'TBL')


dt = readRDS('input/combined_data.rds')[['base']] %>%
  left_join(gw$dt, by = 'ym') %>% 
  drop_na(mkt1) %>% 
  select(ym, rf1, starts_with('mkt'), any_of(topic_list), any_of(gw_list))



#------------------------------------------------------------------------
# Predict
#------------------------------------------------------------------------

yvar      = 'mkt1'
ini_win   = 120
fixed_win = F
ncore     = 10


#----------------------#
#--- ols --------------#
#----------------------#

out_ols = lapply(c(gw_list, topic_list), function(x) {
  fn_os_ols(
    df        = dt,
    xvar      = x,
    yvar      = yvar,
    ini_win   = ini_win,
    fixed_win = fixed_win,
    ncore     = ncore
  )$y
})
names(out_ols) = c(gw_list, topic_list)
saveRDS(out_ols, glue('output/os/ols_{yvar}_{ini_win}.rds'))



#----------------------#
#--- pls --------------#
#----------------------#

Sys.time()
out_pls = lapply(list(gw_list, topic_list, topic_list_12), function(x) {
  fn_os_pls(
    df        = dt,
    xvar      = x,
    yvar      = yvar,
    ini_win   = ini_win,
    fixed_win = fixed_win,
    scale     = FALSE,
    ncore     = ncore
  )$y
})
Sys.time()

names(out_pls) = c('gw', 'topic', 'topic_12')
saveRDS(out_pls, glue('output/os/pls_{yvar}_{ini_win}.rds'))













